// pages/removeRelation/removeRelation.js
let request = require('../../operation/operation.js')
let bxt = require('../../utils/bxt.js')
let teacherSid = null, relationSid = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   * params 
   *  teacherSid:删除老师
   *  relationSid:删除与学生的关系
   */
  onLoad: function (options) {
    if (options.teacherSid) {
      teacherSid = options.teacherSid
    } else if (options.relationSid) {
      relationSid = options.relationSid
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    teacherSid = null
    relationSid = null
  },

  /**
   * 单击解除关系按钮事件处理函数
   */
  onDelRelation:function() {
    let that = this

    wx.showModal({
      title: '解除关系',
      content: '确定解除关系？',
      success: function(res) {
        if (res.confirm) {
          if (teacherSid) {
            that.deleteTeacher()
          } else if (relationSid) {
            that.deleteStudentRelation()
          }
        }
      }
    })
  },

  deleteTeacher:function() {   
    request.deleteRequest('/teachers/' + teacherSid, {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          // 更新用户信息
          getApp().notificationCenter.post(bxt.BIND_ROLE_MESSAGE)
          getApp().autoBack('操作成功')
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      })
      .catch(e => {
        console.log(e)
      })
  },

  deleteStudentRelation:function() {
    var that = this
    request.deleteRequest('/relations/' + relationSid, {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          // 更新用户信息
          getApp().notificationCenter.post(bxt.BIND_ROLE_MESSAGE)
          getApp().autoBack('解除成功')
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      })
      .catch(e => {
        console.log(e)
      })
  }
})